package com.anbinh.doctor;

import java.util.List;
import java.util.Random;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.anbinh.doctor.fragment.FragmentMain;
import com.anbinh.doctor.fragment.lichhen.FragmentLichhen;
import com.anbinh.doctor.fragment.tinnhan.FragmentMessages;
import com.anbinh.doctor.utils.Utils;

public class GcmIntentService extends IntentService {
	public static final int NOTIFICATION_ID = 1000;
	NotificationCompat.Builder builder;
	Random random = new Random();
	public final static String type_appointment = "appointment";
	public final static String type_message = "message";
	public final static String type_promotion = "promotion";

	private String mContent;
	private int mType;
	private String mUrl;
	private int mId;

	public GcmIntentService() {
		super("GcmIntentService");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		try {
			if (intent != null && intent.getExtras() != null) {
				FragmentMain.newInstance().setRefreshAppointMent(true);
				FragmentMain.newInstance().setRefreshMessage(true);

				mContent = intent.getExtras().getString("content");
				if (!intent.getExtras().getString("type").isEmpty())
					mType = Integer.parseInt(intent.getExtras().getString(
							"type"));
				// mUrl = intent.getExtras().getString("url");
				mId = Integer.parseInt(intent.getExtras().getString("id"));
				if (mContent != null) {
					sendNotification(mContent);
					GcmBroadcastReceiver.completeWakefulIntent(intent);
				}

				Log.d("onHandleIntent", "onHandleIntent" + mType + " ");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean isServiceRunning(String pakageName) {
		boolean isServiceFound = false;
		ActivityManager activityManager = (ActivityManager) this
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> services = activityManager
				.getRunningTasks(Integer.MAX_VALUE);

		for (int i = 0; i < services.size(); i++) {
			ComponentName componentInfo = services.get(0).topActivity;
			if (componentInfo.getPackageName().equals(pakageName)) {
				isServiceFound = true;
			}
		}
		return isServiceFound;
	}

	private void sendNotification(String msg) {
		Intent intent = new Intent(this, DetailtActivity.class);
		Bundle data = new Bundle();
		data.putInt(Utils.KEY_ID, mId);
		if (mType == 1) {
			data.putInt(Utils.KEY, R.id.message_detailt);
		} else if (mType == 0) {
			data.putInt(Utils.KEY, Utils.APPOINTMENT_DETAILT);
		}
		intent.putExtras(data);
		PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent,
				Intent.FLAG_ACTIVITY_NEW_TASK);

		// Build notification
		// Actions are just fake
		Notification noti = new Notification.Builder(this)
				.setContentTitle("EasyCare").setContentText(msg)
				.setSmallIcon(R.drawable.ic_lunacher).setContentIntent(pIntent)
				.build();
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		// hide the notification after its selected
		noti.flags |= Notification.FLAG_AUTO_CANCEL;
		noti.defaults |= Notification.DEFAULT_SOUND;
		noti.defaults |= Notification.DEFAULT_VIBRATE;

		notificationManager.notify(random.nextInt(), noti);

	}
}
